package com.example.mobile0123456789hwh0610;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private EditText edtTxtMainName;
    private EditText edtTxtMainAge;
    private EditText edtTxtMainScore;
    private Button btnMainInsert;
    private Button btnMainUpdate;
    private Button btnMainDelete;
    private Button btnMainQuery;
    private TextView tvMainDisplay;
//    声明变量数据库打开助手对象和数据库对象
    MySQLiteOpenHelper openHelper;
    SQLiteDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
//      创建数据库打开助手对象
        openHelper = new MySQLiteOpenHelper(this,"student.db",null,1);
        //添加按钮监听事件
        btnMainInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                获取输入数据
                String name=edtTxtMainName.getText().toString().trim();
                int age = Integer.valueOf(edtTxtMainAge.getText().toString().trim());
                float score = Float.valueOf(edtTxtMainScore.getText().toString().trim());
//                键值对ContentValues对象进行存储
                ContentValues values = new ContentValues();
                values.put("name",name);
                values.put("age",age);
                values.put("score",score);
//                获得可写数据库对象
                database=openHelper.getWritableDatabase();
//                将数据存储到users表
                long result = database.insert("users", null, values);
//                关闭数据库
                database.close();
                Log.i("TAG", "onClick: "+result);
            }
        });
        //查询按钮监听事件
        btnMainQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                获得只读数据库对象
                database=openHelper.getReadableDatabase();
//              将数据读到游标中
                Cursor cursor = database.query("users",
                        null, null, null,
                        null, null, null);
//                调用游标的方法，循环读取数据
                if(cursor.moveToFirst()){//判断有没第一条数据
                    tvMainDisplay.setText(null);//读新数据前，清空显示
                    do{//do循环
                        String name=cursor.getString(1);//读第2列数据，编号为1
                        int age= cursor.getInt(2);
                        float score=cursor.getFloat(3);
                        tvMainDisplay.append(name+"  "+age+"  "+score+"\n");//追加显示
                    }while (cursor.moveToNext());//判断有没下一条数据

                }
//                关闭游标和数据库
                cursor.close();
                database.close();
            }
        });
        //更新按钮监听事件,实现根据姓名，更新成绩
        btnMainUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                获取输入数据
                String name=edtTxtMainName.getText().toString().trim();
                float score = Float.valueOf(edtTxtMainScore.getText().toString().trim());
//                键值对ContentValues存储分数数据
                ContentValues values = new ContentValues();
                values.put("score",score);
//                获取可写数据库对象
                database=openHelper.getWritableDatabase();
//                调用方法，更加姓名，更新数据
                database.update("users",values,"name=?",new String[]{name});
                database.close();
            }
        });
        //删除按钮监听事件,实现根据姓名删除
        btnMainDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                获取输入数据
                String name=edtTxtMainName.getText().toString().trim();
//                获取可写数据库对象
                database=openHelper.getWritableDatabase();
//                调用方法，根据姓名，删除数据
                database.delete("users","name=?",new String[]{name});
                database.close();
            }
        });

    }

    private void initView() {
        edtTxtMainName = (EditText) findViewById(R.id.edtTxtMainName);
        edtTxtMainAge = (EditText) findViewById(R.id.edtTxtMainAge);
        edtTxtMainScore = (EditText) findViewById(R.id.edtTxtMainScore);
        btnMainInsert = (Button) findViewById(R.id.btnMainInsert);
        btnMainUpdate = (Button) findViewById(R.id.btnMainUpdate);
        btnMainDelete = (Button) findViewById(R.id.btnMainDelete);
        btnMainQuery = (Button) findViewById(R.id.btnMainQuery);
        tvMainDisplay = (TextView) findViewById(R.id.tvMainDisplay);
    }
}